package com.example.SpringBoot_demo.service.impl;

import com.example.SpringBoot_demo.bean.Book;
import com.example.SpringBoot_demo.bean.BorrowHistory;
import com.example.SpringBoot_demo.mapper.BookMapper;
import com.example.SpringBoot_demo.mapper.BorrowMapper;
import com.example.SpringBoot_demo.service.BorrowService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BorrowServiceImpl implements BorrowService {

    private static final String INITIALPAGE = "unreturn";

    @Autowired
    private BookMapper bookMapper;
    @Autowired
    private BorrowMapper borrowMapper;

    @Override
    public void insertBorrowHistory(String userid, String username, String bookId) {
        List<Book> book=bookMapper.getBook(bookId,"");
        String bookid=book.get(0).getBookId();
        String bookname=book.get(0).getBookName();
        String authuor=book.get(0).getAuthor();
        System.out.println(bookid);
        System.out.println(bookname);
        System.out.println(authuor);
        int countNumb=borrowMapper.countNumb()+1;
        borrowMapper.insertBorrowHistory(Integer.toString(countNumb),userid,
                                        username,bookid,
                                        bookname,authuor,
                                    "unreturn");
    }

    @Override
    public List<BorrowHistory> myBorrowHistory(String userid,String flag){
        return borrowMapper.myBorrowHistory(userid,flag);
    }

    @Override
    public void returnBook(String bookid,String userid) {
        borrowMapper.returnbook(bookid,userid,"returned");
    }
}
